package com.agent.ai.services;

import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.agent.ai.feign.AIFeignService;
import com.agent.ai.qjsq.domain.OactbdCsqjsq;
import com.agent.ai.qjsq.service.IOactbdCsqjsqService;
import io.micrometer.common.util.StringUtils;
import org.apache.catalina.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Description;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

@Configuration
public class BookingTools {


	@Autowired
	private IOactbdCsqjsqService oactbdCsqjsqService;

	@Autowired
	private AIFeignService aiFeignService;

	// 保存
	public record submitQjsq(String qjsy, String sqr, String sqbm, String sqrq, String loginUserId, String loginUserName, String loginDeptName, String loginDeptId) { }

	// 通过请假数据的id查询请假数据
	public record selectQjById(String businessId) {}

	// 获取请假流程节点数据
	public record nextTasksByNodeId(OactbdCsqjsq oactbdCsqjsq) {}

	// 通过节点id获取当前的用户
	public record getFlowUserByNodeId(String nodeId, OactbdCsqjsq oactbdCsqjsq) {}

	// 流程提交
	public record taskCompleteNew(String nodeId, String nodeName, String submitUserIds, String submitUserNames, OactbdCsqjsq oactbdCsqjsq) {}


	@Bean
	@Description("保存请假数据")
	public Function<submitQjsq, Object> submitQjsqUser() {
		return request -> {
			String id = IdUtil.simpleUUID();
			OactbdCsqjsq oactbdCsqjsq = new OactbdCsqjsq();
			oactbdCsqjsq.setId(id);   // 设置id
			oactbdCsqjsq.setTitle(request.qjsy);  // 设置请假事由
			oactbdCsqjsq.setSqr(request.sqr);  // 设置请假人
			oactbdCsqjsq.setSqrq(request.sqrq);  // 设置请假日期
			oactbdCsqjsq.setSqrbm(request.sqbm);  // 设置请假人部门
			oactbdCsqjsq.setCreateusername(request.loginUserName);
			oactbdCsqjsq.setCreateuserid(request.loginUserId);
			oactbdCsqjsq.setCreatedeptname(request.loginDeptName);
			oactbdCsqjsq.setCreatedeptid(request.loginDeptId);
			oactbdCsqjsq.setCreateTime(new Date());
			oactbdCsqjsq.setMjid("10");
			oactbdCsqjsq.setMjname("公开");
			oactbdCsqjsq.setYwlx("csqjsq");
			oactbdCsqjsq.setYwname("测试请假申请");
			oactbdCsqjsq.setFormversion("1");
			oactbdCsqjsq.setStatus("10");
			oactbdCsqjsqService.insertOactbdCsqjsq(oactbdCsqjsq);
			return oactbdCsqjsq;
		};
	}

	@Bean
	@Description("通过请假id查询请假数据")
	public Function<selectQjById, Object> selectQjByIdNew() {
		return request -> {
			OactbdCsqjsq oactbdCsqjsq = oactbdCsqjsqService.selectOactbdCsqjsqById(request.businessId);
			return oactbdCsqjsq;
		};
	}


	@Bean
	@Description("获取请假流程节点列表")
	public Function<nextTasksByNodeId, JSONArray> nextTasksByNodeIdNew() {
		return request -> {
			// 通过请假数据id查询数据
			String nodeId = "";
			String processDefinitionKey = "csqjsq";
			JSONArray obj = aiFeignService.nextTasksByNodeId(nodeId, processDefinitionKey);
			return obj;
		};
	}

	@Bean
	@Description("获取请假流程某个节点人员列表")
	public Function<getFlowUserByNodeId, JSONArray> getFlowUserByNodeIdNew() {
		return request -> {
			// 通过请假数据id查询数据
			OactbdCsqjsq oactbdCsqjsq = request.oactbdCsqjsq;
			String nodeId = request.nodeId;
			// 人员信息
			JSONArray userList = aiFeignService.getFlowUserByNodeId(nodeId, "csqjsq", oactbdCsqjsq.getCreateuserid(), oactbdCsqjsq.getCreateuserid());
			return userList;
		};
	}

	@Bean
	@Description("提交请假流程")
	public Function<taskCompleteNew, Object> taskComplete() {
		return request -> {
			String nodeId = request.nodeId;
			String nodeName = request.nodeName;
			String userIds = request.submitUserIds;
			String userNames = request.submitUserNames;
			OactbdCsqjsq oactbdCsqjsq = request.oactbdCsqjsq;
			// 调用流程启动接口
			String businessKey = oactbdCsqjsq.getId();
//			String processDefinitionKey = oactbdCsqjsq.getYwlx();
			String processDefinitionKey = "csqjsq";
			String title = oactbdCsqjsq.getTitle();
//			String moduleId = oactbdCsqjsq.getYwlx();
			String moduleId = "csqjsq";
			String firstNode = nodeId;
			String reportStatus = "ptyg";
//			String mijiid = oactbdCsqjsq.getMjid();
			String mijiid = "10";
			String tableName = "";
			String comminfoId = "";
//			String formTyp = oactbdCsqjsq.getFormtype();
			String formTyp = "6";
			// 流程启动
			Map<String, Object> flowStartMap = aiFeignService.startProcessByKeyNew(businessKey, processDefinitionKey, title, moduleId, firstNode, reportStatus, mijiid, tableName, comminfoId, formTyp);
			// 获取任务信息
			Map<String, Object> taskInfo = (Map<String, Object>) flowStartMap.get("taskInfo");
			if (taskInfo != null) {
				String taskId = taskInfo.get("taskId").toString();
				// 修改流程状态
				Boolean b = aiFeignService.updateReportStatus(taskId, "tjz");
				// 如果修改成功，则流程提交
				if (b) {
					String currentTaskId = taskId;
					String nextTaskActivityId = nodeId;
					String nextTaskUserIds = userIds;
					String nextTaskUsernames = userNames;
					String assigneeId = userIds;
					String nodeCode = "";
					String rwlxs = "ptyg";
					String exField5 = "true";
					String processDefinitionId = "";
					String currentNodeId = "";
					String tjlx = "";
					String logtype = "";
					// 提交流程
					Map<String, Object> map = aiFeignService.taskCompleteNew(
							businessKey,
							currentTaskId,
							nextTaskActivityId,
							nextTaskUserIds,
							nextTaskUsernames,
							assigneeId,
							moduleId,
							nodeName,
							reportStatus,
							nodeCode,
							rwlxs,
							exField5,
							processDefinitionId,
							currentNodeId,
							tjlx,
							logtype,
							tableName,
							comminfoId,
							processDefinitionKey,
							formTyp
					);
				}
			}

			return "流程已经提交";
		};
	}



}
